//
//  SearchViewControllerTransition.swift
//  elarc01
//
//  Created by elarc on 16/3/21.
//  Copyright © 2016年 elarc. All rights reserved.
//

import Foundation

class  SearchViewControllerTransition:NSObject, UIViewControllerTransitioningDelegate {
    
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return SearchDisplayPresentAnimator()
    }
    
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return SearchDisplayDismissAnimator()
    }
}

class  SearchDisplayPresentAnimator:NSObject,UIViewControllerAnimatedTransitioning {
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let toNavigationVc = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) as? BaseNavigationController
        let toViewController = toNavigationVc!.topViewController as? SearchIPsViewController
        transitionContext.containerView()!.addSubview(toNavigationVc!.view)
        toNavigationVc?.view.alpha = 0
        if ((toViewController?.isKindOfClass(SearchIPsViewController)) == true) {
            toViewController?.searchBgView.frame.origin.y = 64
            toViewController?.searchBar.frame.size.width = ScreenWidth
            toViewController?.canCelBtn.frame.origin.x = ScreenWidth + 50
        }
        toNavigationVc?.setNavigationBarHidden(true, animated: false)
        UIView.animateWithDuration(self.transitionDuration(transitionContext), animations: { () -> Void in
            toNavigationVc?.view.alpha = 0
            if ((toViewController?.isKindOfClass(SearchIPsViewController)) == true) {
                toViewController?.searchBgView.frame.origin.y = 20
                toViewController?.searchBar.frame.size.width = ScreenWidth - (toViewController?.canCelBtn.width)!
                toViewController?.canCelBtn.frame.origin.x = ScreenWidth - (toViewController?.canCelBtn.width)!
            }
            }) { (Bool) -> Void in
                transitionContext.completeTransition(transitionContext.transitionWasCancelled())
                if ((toViewController?.isKindOfClass(SearchViewController)) == true)&&(toViewController?.searchBar.canBecomeFirstResponder()==true){
                    toViewController?.searchBar.becomeFirstResponder()
                }
        }
    }
}

class  SearchDisplayDismissAnimator:NSObject,UIViewControllerAnimatedTransitioning {
        func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
            return 0.5
        }
        func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
            let fromNav = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) as? UINavigationController
            let fromViewController = fromNav?.topViewController as? SearchIPsViewController
            let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
            fromNav?.view.alpha = 1
            if ((fromViewController?.isKindOfClass(SearchIPsViewController)) == true) {
                fromViewController!.view.bringSubviewToFront((fromViewController?.searchBar)!)
                fromViewController?.searchBgView.frame.origin.y = 20
                fromViewController?.searchBar.frame.size.width = ScreenWidth - (fromViewController?.canCelBtn.width)!
                fromViewController?.canCelBtn.frame.origin.x = ScreenWidth - (fromViewController?.canCelBtn.width)!
            }
            toViewController?.view.alpha = 0.0
            UIView.animateWithDuration(self.transitionDuration(transitionContext), animations: { () -> Void in
                if ((fromViewController?.isKindOfClass(SearchIPsViewController)) == true) {
                    fromViewController?.searchBgView.frame.origin.y = 64
                    fromViewController?.searchBar.frame.size.width = ScreenWidth
                    fromViewController?.canCelBtn.frame.origin.x = ScreenWidth + 50
                }
                fromViewController?.view.alpha = 0.0
                toViewController?.view.alpha = 1
                }) { (Bool) -> Void in
                    transitionContext.completeTransition(transitionContext.transitionWasCancelled())
                }
        }

}